//
//  ServerListViewController.m
//  魔兽大数据
//
//  Created by 王右 on 2017/11/27.
//  Copyright © 2017年 王右. All rights reserved.
//

#import "ServerListViewController.h"
#import "ServerModel.h"
#import "ServerListViewCell.h"
#import "BigDataHeader.h"

@interface ServerListViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

@property (weak, nonatomic) IBOutlet UITableView *baseTableView;
@property (nonatomic, strong) NSArray *modelArr;
@property (strong, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (nonatomic, strong) NSMutableArray *searchReasultArr;
@property (weak, nonatomic) IBOutlet UIButton *backToTopButton;
@property (nonatomic, strong) NSString *searchString;
@end

@implementation ServerListViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_baseTableView reloadData];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的服务器";
    _backToTopButton.layer.cornerRadius = _backToTopButton.bounds.size.width / 2;
    _baseTableView.tableHeaderView = _headerView;
    [RequestManager requestGetBnadeWithModule:@"realms" param:nil model:@"ServerModel" success:^(NSArray *obj) {
        _modelArr = obj;
        _searchReasultArr=_modelArr.mutableCopy;
        [_baseTableView reloadData];
    } failure:^(NSString *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _searchReasultArr.count;
}

- (CGFloat )tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ServerListViewCell *cell = (ServerListViewCell *)[tableView cellWithCellNibName:@"ServerListViewCell"];
    [cell setModel:_searchReasultArr[indexPath.row] rank:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ServerModel *model = _searchReasultArr[indexPath.row];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[CCCache defaultManager] setObject:model forKey:@"myHomeServer"];
        [_baseTableView reloadData];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消"
                                                      style:UIAlertActionStyleCancel
                                                    handler:nil];
    UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"设置所属服务器" message:[NSString stringWithFormat:@"%@",model.targetServerName] preferredStyle:UIAlertControllerStyleAlert];
    [controller addAction:action1];
    [controller addAction:action2];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:controller animated:YES completion:nil];
    });
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.view endEditing:YES];
    
    if (scrollView.contentOffset.y > self.view.bounds.size.height) {
        _backToTopButton.hidden = NO;
    }else{
        _backToTopButton.hidden = YES;
    }
    
    
    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    NSString *searchString = searchBar.text;
    //搜索关键字为空
    if (searchString.length == 0) {
        _searchReasultArr = _modelArr.mutableCopy;
        [_baseTableView reloadData];
        return;
    }
    _searchString = searchString;
    __block NSMutableArray *listArr = [NSMutableArray array];
    [_modelArr enumerateObjectsUsingBlock:^(ServerModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.targetServerName containsString:searchString]) {
            [listArr addObject:obj];
        }
    }];
    if (listArr.count > 0) {
        [_searchReasultArr removeAllObjects];
        [_searchReasultArr addObjectsFromArray:listArr];
        [_baseTableView reloadData];
    }
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    NSString *searchString = searchBar.text;
    //搜索关键字为空
    if (searchString.length == 0) {
        _searchReasultArr = _modelArr.mutableCopy;
        [_baseTableView reloadData];
        return;
    }
    _searchString = searchString;
    __block NSMutableArray *listArr = [NSMutableArray array];
    [_modelArr enumerateObjectsUsingBlock:^(ServerModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj.targetServerName containsString:searchString]) {
            [listArr addObject:obj];
        }
    }];
    if (listArr.count > 0) {
        [_searchReasultArr removeAllObjects];
        [_searchReasultArr addObjectsFromArray:listArr];
        [_baseTableView reloadData];
    }
}

- (IBAction)backToTopButtonAction:(id)sender {
    NSIndexPath* indexPat = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.baseTableView scrollToRowAtIndexPath:indexPat atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}


- (NSMutableArray *)searchReasultArr{
    if (_searchReasultArr == nil) {
        _searchReasultArr = [NSMutableArray array];
        return _searchReasultArr;
    }
    return _searchReasultArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
